package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wallet.dao.ExchangeMoneyInfoDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;

import cn.wp.ewallet.wallet.model.entity.ExchangeMoneyInfoDo;
import cn.wp.ewallet.wallet.model.entity.ExchangeMoneyInfoDoo;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 兑换申请单-数据访问层实现
 * Created by zhangyongli on 2020/01/09
 */
@Repository
public class ExchangeMoneyInfoDaoImpl implements ExchangeMoneyInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public ExchangeMoneyInfoDoo selectExchangeMoneyReqOrderById(String orderId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, p.oper_result, s.sys_user_code as oper_user, p.oper_time, p.remark as oper_remark");
        sqlString.add("    , ep.conver_type, ep.exchange_amount, ep.exchange_rate");
        sqlString.add("from (");
        sqlString.add("    select r.*, e.turn_money_type, e.money_amount, e.exchange_money_type, m1.money_type_code as turn_money_type_name");
        sqlString.add("        , m2.money_type_code as exchange_money_type_name");
        sqlString.add("    from req_order_info r, exchange_money_info e, money_type_info m1, money_type_info m2");
        sqlString.add("    where r.req_order_id = e.req_order_id");
        sqlString.add("        and e.exchange_money_type = m2.money_type_id");
        sqlString.add("        and e.turn_money_type = m1.money_type_id");
        sqlString.add("        and r.req_order_id = ?");
        sqlString.add(") t");
        sqlString.add("    left join proc_record_info p on t.req_order_id = p.req_order_id");
        sqlString.add("    left join exchange_proc_info ep on p.proc_record_id = ep.proc_record_id");
        sqlString.add("    left join system_user_info s on p.oper_user = s.sys_user_id");

        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[] {orderId}, ExchangeMoneyInfoDoo.class);
    }

    @Override
    public WpPage<ExchangeMoneyInfoDoo> selectExchangeMoneyReqOrderByaddress(String address, WpPage page) {

        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, p.oper_result, s.sys_user_code as oper_user, p.oper_time, p.remark as oper_remark");
        sqlString.add("    , ep.conver_type, ep.exchange_amount, ep.exchange_rate");
        sqlString.add("from (");
        sqlString.add("    select r.*, e.turn_money_type, e.money_amount, e.exchange_money_type, m1.money_type_code as turn_money_type_name");
        sqlString.add("        , m2.money_type_code as exchange_money_type_name");
        sqlString.add("    from req_order_info r, exchange_money_info e, money_type_info m1, money_type_info m2");
        sqlString.add("    where r.req_order_id = e.req_order_id");
        sqlString.add("        and e.exchange_money_type = m2.money_type_id");
        sqlString.add("        and e.turn_money_type = m1.money_type_id");
        sqlString.add("        and r.wallet_address = ?");
        sqlString.add("        and r.req_type = ?");
        sqlString.add(") t");
        sqlString.add("    left join proc_record_info p on t.req_order_id = p.req_order_id");
        sqlString.add("    left join exchange_proc_info ep on p.proc_record_id = ep.proc_record_id");
        sqlString.add("    left join system_user_info s on p.oper_user = s.sys_user_id");
        sqlString.add("order by t.create_date desc");

        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[] {address, EwalletWalletConstant.REQ_ORDER_TYPE_EXCHANGEMONEY}, page, ExchangeMoneyInfoDoo.class);
    }

    @Override
    public void insertExchangeMoneyInfo(ExchangeMoneyInfoDo exchangeMoneyInfoDo) {
        jdbcSupportTemplate.insert(exchangeMoneyInfoDo);
    }
}